/*
 * Copyright (c) 2009 XMock contributors
 * This program is made available under the terms of the Apache License.
 */
package org.xmock.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.xmock.exception.MockException;


class FactoryFinder {
	
	private static final Logger logger = Logger.getLogger(FactoryFinder.class);
	
    FactoryFinder()
    {
    }

    private static Object newInstance(String s, ClassLoader classloader) throws MockException
    {
        try
        {
            Class class1;
            if(classloader == null)
                class1 = Class.forName(s);
            else
                class1 = classloader.loadClass(s);
            return class1.newInstance();
        }
        catch(ClassNotFoundException classnotfoundexception)
        {
            throw new MockException("Provider " + s + " not found", classnotfoundexception);
        }
        catch(Exception exception)
        {
            throw new MockException("Provider " + s + " could not be instantiated: " + exception, exception);
        }
    }

    static Object find(String s) throws MockException
    {
        ClassLoader classloader;
        try
        {
            classloader = Thread.currentThread().getContextClassLoader();
        }
        catch(Exception exception)
        {
            throw new MockException(exception.toString(), exception);
        }
        try
        {
            String s1 = System.getProperty(s);
            if(s1 != null)
                return newInstance(s1, classloader);
        }
        catch(SecurityException securityexception) { }
        try
        {
            String s2 = System.getProperty("java.home");
            String s4 = s2 + File.separator + "lib" + File.separator + "xmock.properties";
            File file = new File(s4);
            if(file.exists())
            {
                Properties properties = new Properties();
                properties.load(new FileInputStream(file));
                String s6 = properties.getProperty(s);
                return newInstance(s6, classloader);
            }
        }
        catch(Exception exception1) { }
        String s3 = "META-INF/xmock/" + s;
        try
        {
            java.io.InputStream inputstream = null;
            if(classloader == null)
                inputstream = ClassLoader.getSystemResourceAsStream(s3);
            else
                inputstream = classloader.getResourceAsStream(s3);
            if(inputstream != null)
            {
                BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(inputstream, "UTF-8"));
                String s5 = bufferedreader.readLine();
                bufferedreader.close();
                if(s5 != null && !"".equals(s5))
                    return newInstance(s5, classloader);
            }
        }
        catch(Exception exception2) { }
        String s7 = "META-INF/xmock/xmock.properties";
        try
        {
            java.io.InputStream inputstream = null;
            if(classloader == null)
                inputstream = ClassLoader.getSystemResourceAsStream(s7);
            else
                inputstream = classloader.getResourceAsStream(s7);
            if(inputstream != null)
            {
                Properties properties = new Properties();
                properties.load(inputstream);
                String s8 = properties.getProperty(s);
                return newInstance(s8, classloader);
            }
        }
        catch(Exception exception2) { }
        return null;
    }

    static Object find(String s, String s1) throws MockException
    {
        Object obj = find(s);
        if(obj != null)
            return obj;
        ClassLoader classloader;
        try
        {
            classloader = Thread.currentThread().getContextClassLoader();
        }
        catch(Exception exception)
        {
            throw new MockException(exception.toString(), exception);
        }
        if(s1 == null)
            throw new MockException("Provider for " + s + " cannot be found", null);
        else
            return newInstance(s1, classloader);
    }
}
